package edu.friday.controller;

import edu.friday.common.base.BaseController;
import edu.friday.common.constant.UserConstants;
import edu.friday.common.result.RestResult;
import edu.friday.common.result.TableDataInfo;
import edu.friday.model.SysRole;
import edu.friday.model.vo.SysRoleVO;
import edu.friday.model.vo.SysUserVO;
import edu.friday.service.ISysRoleService;
import edu.friday.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/system/role")
@Validated
public class SysRoleController extends BaseController {

    @Autowired
    private ISysRoleService iSysRoleService;

    @GetMapping("/list")
    public TableDataInfo selectAllRole(SysRoleVO sysRoleVO, Pageable pageable) {
        int pageNumber = pageable.getPageNumber() - 1;
        pageNumber = Math.max(pageNumber, 0);
        pageable = PageRequest.of(pageNumber, pageable.getPageSize());
        return iSysRoleService.selectRoleList(sysRoleVO, pageable);
    }

    @PostMapping
    public RestResult add(@RequestBody @Validated SysRoleVO sysRoleVO) {
        if (UserConstants.NOT_UNIQUE.equals(iSysRoleService.checkRoleNameUnique(sysRoleVO)))
            return RestResult.error(sysRoleVO.getRoleName() + "角色名已存在");
        else if (UserConstants.NOT_UNIQUE.equals(iSysRoleService.checkRoleKeyUnique(sysRoleVO)))
            return RestResult.error(sysRoleVO.getRoleKey() + "角色权限已存在");
        sysRoleVO.setCreateBy("system");
        return toAjax(iSysRoleService.insertRole(sysRoleVO) ? 1 : 0);
    }

    @DeleteMapping("/{roleIds}")
    public RestResult deleteUser(@PathVariable Long[] roleIds) {
        return toAjax(iSysRoleService.deleteRoleByIds(roleIds));
    }

    @PutMapping
    public RestResult edit(@RequestBody @Validated SysRoleVO sysRoleVO) {
        if (UserConstants.NOT_UNIQUE.equals(iSysRoleService.checkRoleNameUnique(sysRoleVO)))
            return RestResult.error(sysRoleVO.getRoleName() + "角色名已存在");
        else if (UserConstants.NOT_UNIQUE.equals(iSysRoleService.checkRoleKeyUnique(sysRoleVO)))
            return RestResult.error(sysRoleVO.getRoleKey() + "角色权限已存在");
        sysRoleVO.setCreateBy("system");
        return toAjax(iSysRoleService.updateRole(sysRoleVO));
    }

    @GetMapping("/{roleId}")
    public RestResult getInfo(@PathVariable Long roleId) {
        SysRole sysRole = iSysRoleService.selectRoleById(roleId);
        if (StringUtils.isNotNull(sysRole))
            return RestResult.success(sysRole);
        return RestResult.error("请检查roleId或角色已被删除");
    }

    @PutMapping("/changeStatus")
    public RestResult updateStatus(@RequestBody @Validated SysRoleVO sysRoleVO) {
        sysRoleVO.setUpdateBy("system");
        return toAjax(iSysRoleService.updateStatus(sysRoleVO) ? 1 : 0);
    }
}
